﻿using System;
using Framework.MVC;
using Log.Model;
using Collection.Model;
using Collection.Model.Behavior;
using Collection.IService.Entry;
using Config.Model;
using System.Collections.Generic;
using Framework.MVVM.Model;
using Config.IService;
using Log.IService;
using Common.IService;
using Config.Service;
using Log.Service;
using Common.Service;
using Collection.Service;
using Collection.IService;

namespace CollectionCenter.Models
{
    [ServiceAttribute(typeof(IMarkService), typeof(MarkService), "Instance")]
    [ServiceAttribute(typeof(IConfigService), typeof(ConfigService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewMarkModel : AsyncModelBase, IWriteLog, INewMark, IGetConfig<IDictionary<string, string>>
    {
        #region Field
        private const string m_error_config = "newmark00";
        private const string m_complete_config = "newmark01";
        private const string m_error = "newmark02";
        private const string m_complete = "newmark03";

        private const string m_name = "data:mark:source:white";
        private const string m_entry = "entry";
        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            ConfigName = m_name;
            this.GetConfig(m_error_config, m_complete_config);
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion IWriteLog

        #region IGetConfig

        public string ConfigName{ get; set; }

        public void OnGetConfigComplete(string key, IDictionary<string, string> entry)
        {
            if (entry.ContainsKey(MarkSourceCode))
            {
                OnBeginRequest();
                this.NewMark(m_error, m_complete);
            }
            this.Log(key, new MarkEntry { code = MarkCode, indate = MarkIndate, source = MarkSource, sourcecode = MarkSourceCode });
        }

        public void OnGetConfigError(string key)
        {
            this.Log(key, new MarkEntry { code = MarkCode, indate = MarkIndate, source = MarkSource, sourcecode = MarkSourceCode });
        }

        #endregion IGetConfig

        #region INewMark

        public string MarkCode { get; set; }

        public string MarkIndate { get; set; }

        public string MarkSource { get; set; }

        public string MarkSourceCode { get; set; }

        public void OnNewMarkComplete(string key, MarkEntry entry)
        {
            OnComplete(m_entry, entry);
            this.Log(key, entry);
        }

        public void OnNewMarkError(string key)
        {
            this.Log(key, new MarkEntry { code = MarkCode, indate = MarkIndate, source = MarkSource, sourcecode = MarkSourceCode });
        }

        #endregion INewMark

    }
}